

package yf.mall.service.impl;
import com.yf.utils.ShiroUtils;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.dao.*;
import yf.mall.entity.ShopEntity;
import yf.mall.entity.ShopLogRuleEntity;
import  yf.mall.vo.down.ShopLogTemDown;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import yf.mall.vo.down.ShopLogTemDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.entity.ShopLogTemEntity;
import yf.mall.vo.ShopLogTemVo;
import yf.mall.service.ShopLogTemService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Aug 20 10:19:54 CST 2020
 */
@Service
public class ShopLogTemServiceImpl extends BaseServiceImpl<ShopLogTemEntity> implements ShopLogTemService {

    private ShopLogTemDao dao;


    @Autowired
    public void setDao(ShopLogTemDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }

    @Autowired
    private ShopLogRuleDao logRuleDao;
    @Autowired
    private SysUsersDao sysUsersDao;
    @Autowired
    private ShopDao shopDao;


    @Override
    public List<ShopLogTemVo> findVo(ShopLogTemVo vo){
        return dao.findVo(vo);
    }

    @Override
    public ShopLogTemVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(ShopLogTemVo vo){
        //添加运费设置
        saveOrUpdate(vo.getShopLogTem(),vo.getShopLogStatus());
        int tem = dao.save(vo.getEntity());
        if (vo.getShopLogRules() != null && vo.getShopLogRules().size() > 0) {
            vo.getShopLogRules().forEach(logRule -> {
                logRule.setLogId(vo.getShopLogTem().getId());
                this.shopLogRuleDao.save(logRule);
            });
        }
        return tem;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(ShopLogTemVo vo){
        //修改运费设置
        saveOrUpdate(vo.getShopLogTem(),vo.getShopLogStatus());

        if (vo.getShopLogRules() != null && vo.getShopLogRules().size() > 0) {
            logRuleDao.deleteByLogId(vo.getShopLogTem().getId());
            for (ShopLogRuleEntity logRule : vo.getShopLogRules()) {
                logRule.setLogId(vo.getShopLogTem().getId());
                this.shopLogRuleDao.save(logRule);
            }
        }
        return dao.update(vo.getEntity());
    }

    private void saveOrUpdate(ShopLogTemEntity shopLogTemEntity,Integer shopLogStatus) {
        //获取店铺
        Long sysUser_id = ShiroUtils.getUserId();
        Long userId = sysUsersDao.findBySysUserId(sysUser_id);
        //获取该店铺的运费设置
        if (userId != null) {
            ShopEntity shopEntity = shopDao.findByUserId(userId);
            Integer status = dao.getStatus(shopEntity.getId());
            shopLogTemEntity.setShopId(shopEntity.getId());
            //为空在保存运费设置
            if (status == null) {
                dao.saveShopStatus(shopEntity.getId(), shopLogStatus);
            } else {
                dao.updateByShopId(shopEntity.getId(), shopLogStatus);
            }
        }
    }

    @Override
    public void downLoad(HttpServletResponse response, List<ShopLogTemVo> list) {
        try {
            List<ShopLogTemDown> datas = list.stream().map(item -> new ShopLogTemDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), ShopLogTemDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**内部方法**/
    @Autowired
    ShopLogRuleDao shopLogRuleDao;
    @Override
    public List<ShopLogTemEntity> getList( Long shopId) {
        List<ShopLogTemEntity> temEntityList = dao.findByShopId(shopId);
        for (ShopLogTemEntity shopLogTemEntity : temEntityList) {
            List<ShopLogRuleEntity> ruleEntityList = shopLogRuleDao.findByTemId(shopLogTemEntity.getId());
            shopLogTemEntity.setShopLogRuleEntityList(ruleEntityList);
        }
        return temEntityList;
    }

    @Override
    public Integer getStatus(Long shopId) {
        return dao.getStatus(shopId);
    }


    /**外部方法实现**/




}
